package com.pi.wms.service;

import com.pi.common.utils.StringUtils;
import com.pi.framework.core.repository.IBaseRepository;
import com.pi.framework.core.service.impl.BaseCrudService;
import com.pi.wms.entity.TwiDocMesKitDocline;
import com.pi.wms.repository.MesKitDoclineRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Transactional
@Service
public class MesKitDocklineService extends BaseCrudService<TwiDocMesKitDocline, Long>{

	@Autowired
	protected MesKitDoclineRepository repository;

    @Override
	protected IBaseRepository<TwiDocMesKitDocline,Long> getRepository() {
		return this.repository;
	}
	
	@Override
	protected Predicate toPredicate(Root<TwiDocMesKitDocline> root, CriteriaQuery<?> cq, CriteriaBuilder cb, Map<String, ?> query) {
		List<Predicate> list = new ArrayList<Predicate>();

		String divisionid = (String) query.get("divisionid");
		if (!StringUtils.isNullOrBlank(divisionid)) {
			list.add(cb.equal(root.get("divisionId"), divisionid));
		}

		String mat = (String) query.get("material");
        if (!StringUtils.isNullOrBlank(mat)) {
            list.add(cb.or(cb.like(root.get("materialCode"), "%" + mat.trim() + "%"), cb.like(root.get("materialName"), "%" + mat.trim() + "%")));
		}

        String wbs = (String) query.get("wbsnum");
        if (!StringUtils.isNullOrBlank(wbs)) {
            list.add(cb.like(root.get("wbsNum"), "%"+wbs.trim()+"%"));
        }

        String sup = (String) query.get("supplier");
        if (!StringUtils.isNullOrBlank(sup)) {
            list.add(cb.or(cb.like(root.get("supplierCode"), "%" + sup.trim() + "%"), cb.like(root.get("supplierName"), "%" + sup.trim() + "%")));
        }

        String status = (String) query.get("status");
        if (!StringUtils.isNullOrBlank(status)) {
            list.add(cb.equal(root.get("lineStatus"), status.trim()));
        }
		Predicate[] p = new Predicate[list.size()];
		return cb.and(list.toArray(p));
	}

	public List<TwiDocMesKitDocline> query(String documentNumber){
        if (StringUtils.isNullOrBlank(documentNumber)){
            return new ArrayList<>();
        }
        return repository.findByDocumentNumber(documentNumber);
	}

    /**
     *  清除历史数据
     */
	public String cleanAll(String documentNumber){
        String m = "";
        if (StringUtils.isNullOrBlank(documentNumber)){
           return m;
        }
        int count=0;
        try{
            count=this.repository.delete(documentNumber);
        }catch (Exception e){
            m="清除历史数据异常.异常:"+e.getMessage();
            e.printStackTrace();
        }
        return m;
    }


}
